// pages/user-message/user-message.js
var app = getApp();
Page({
   
  data: {
    currentTab: 0,
    pageSize: 10,//每页数量
    pageNum: 1,//页数
    userId: wx.getStorageSync('userid'),//医生id
    dataList:[],//数据列表
    name:"",
    msgId:"",// 互动提醒消息id
    imgUrl: 'https://tyc-bj.cn/yc/api/attach/',
    pageName: 'message', // 控制tabbar图标
    isshowadd: 0, // 控制是否显示添加按钮
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    this.xianshi();
  },
  //滑动切换
  swiperTab: function (e) {
    var that = this;
    that.setData({
      currentTab: e.detail.current
    }); 
    that.getDataListByTab();
  },
  //点击切换
  clickTab: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      });
      that.getDataListByTab();
    }
  },
  //获取数据
  getDataListByTab:function(){

    const that = this;
    const { currentTab } = that.data;

    if (currentTab == 0) {
      // 互动提醒
      wx.request({
        url: app.globalData.baseUrl + 'doctor/usermsg/getMsgByUser',
        data: {
          pageSize: that.data.pageSize,//每页数量
          pageNum: that.data.pageNum,//页数
          userId: that.data.userId,//互动用户id
        },
        header: {
          'sign': app.globalData.sign,
          'token': app.globalData.token,
          'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
        },
        method: 'POST',
        success: function (t) {
          console.log("互动消息", t.data);
          var guanzhu = 
          
          that.setData({
            dataList: t.data.data
          })
          
        }
      });
    } else if (currentTab == 1) {
      // 行业新闻
      wx.request({
        url: app.globalData.baseUrl + 'system/newsapi/getnewsbystate',
        data: {
          pageSize: that.data.pageSize,//每页数量
          pageNum: that.data.pageNum,//页数
          doctorId: that.data.userId,//医生id
        },
        header: {
          'sign': app.globalData.sign,
          'token': app.globalData.token,
          'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
        },
        method: 'POST',
        success: function (t) {
          console.log("行业新闻", t.data);
          that.setData({
            dataList: t.data.data
          })
        }
      });
    } else if (currentTab == 2) {
      // 医生平台新闻
      wx.request({
        url: app.globalData.baseUrl + 'system/newsapi/getnews',
        data: {
          pageSize: that.data.pageSize,//每页数量
          pageNum: that.data.pageNum,//页数
          doctorId: that.data.userId,//医生id
        },
        header: {
          'sign': app.globalData.sign,
          'token': app.globalData.token,
          'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
        },
        method: 'POST',
        success: function (t) {
          console.log("医生平台新闻", t);
          that.setData({
            dataList: t.data.data
          })
        }
      });
    } else if (currentTab == 3) {
      //订单消息
      wx.request({
        url: app.globalData.baseUrl + 'shopp/orderNews/getOrderNewsPage?pagenum=' + that.data.pageNum + '&pagesize=' + that.data.pageSize,
        data: {},
        header: {
          'sign': app.globalData.sign,
          'token': app.globalData.token,
          // 'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
          'content-type': 'application/json',
        },
        success: function (t) {
          console.log("医订单提醒", t);
          that.setData({
            dataList: t.data.data.orderNews
          })
        }
      })
    } else if (currentTab == 4) {
      //我的咨询
      wx.request({
        url: app.globalData.baseUrl + 'consult/talkapi/gettalkpage',
        data: {
          pageSize: that.data.pageSize,//每页数量
          pageNum: that.data.pageNum,//页数
          type: 2,//医生端
        },
        header: {
          'sign': app.globalData.sign,
          'token': app.globalData.token,
          'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
        },
        method: 'POST',
        success: function (t) {
          console.log("医生我的咨询", t);
          that.setData({
            dataList: t.data.data
          })
        }
      })
    } else {
      return
    }
  },

  //订单跳转 1是术前术后  2是医美
  jop: function (e) {
    console.log("zhi3", e.currentTarget.dataset)
    var that = this;
    var orderType = e.currentTarget.dataset.ordertype;
    var orderId = e.currentTarget.dataset.orderid;

    if (orderType == 1) {
      wx.navigateTo({
        url: '../user-my-order-details/user-my-order-details?id=' + orderId + '&orderType=' + orderType,
      })
    } else {
      wx.navigateTo({
        url: '../user-my-order-details-medical-1/user-my-order-details-medical-1?id=' + orderId + '&orderType=' + orderType,

      })
    }
  },

  //删除互动提醒
  deleteClick: function (event) {
    console.log("俩id", event)
    var that = this;
    var userId = event.currentTarget.dataset.userid
    var msgId = event.currentTarget.dataset.id
    wx.request({
      url: app.globalData.baseUrl + 'doctor/usermsg/deleteMsg',
      data: {
        userId: userId, //互动用户id
        msgId: msgId,
      },
      header: {
        'sign': app.globalData.sign,
        // 'token': app.globalData.token,
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      method: 'POST',
      success: function (res) {
        console.log("删除互动消息", res);
        if (res.data.status == 0) {
          wx.showToast({
            title: "失败",
            icon: 'loading',
            duration: 1500
          })
        } else {
          wx.showToast({
            title: "成功",
            icon: 'success',
            duration: 1000
          })

          that.onLoad();

        }
      },
      fail: function () {
        wx.showToast({
          title: '服务器网络错误!',
          icon: 'loading',
          duration: 1500
        })
      }


    })
  },

  //删除订单提醒id
  deleteorder: function (e) {
    console.log("删除订单提醒id", e.currentTarget.dataset)
    var that = this;
    var id = e.currentTarget.dataset.id
    var url = app.globalData.baseUrl + 'shopp/orderNews/deleteOrderNews/' + id;
    console.log("sad12", url)
    wx.request({
      url: url,
      header: {
        'sign': app.globalData.sign,
        'token': app.globalData.token,
        // 'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
        'content-type': 'application/json'
      },
      method: 'GET',
      success: function (res) {
        console.log("删除订单提醒", res);
        wx.showToast({
          title: "成功",
          icon: 'success',
          duration: 1000
        })
        that.getDataListByTab();
      },
      fail: function () {
        wx.showToast({
          title: '服务器网络错误!',
          icon: 'loading',
          duration: 1500
        })
      }
    })
  },
  //默认显示
  xianshi:function(){
    var that = this;
    wx.request({
      url: app.globalData.baseUrl + 'doctor/usermsg/getMsgByUser',
      data: {
        pageSize: that.data.pageSize,//每页数量
        pageNum: that.data.pageNum,//页数
        userId: that.data.userId,//互动用户id
      },
      header: {
        'sign': app.globalData.sign,
        // 'token': app.globalData.token,
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      method: 'POST',
      success: function (t) {
        that.setData({
          dataList: t.data.data
        })
      }
    })
  }
})
